package job.manager.client.mb;

import java.util.ArrayList;
import java.util.Collection;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.model.SelectItem;
import job.manager.db.ec.UserGroupsEB;
import job.manager.db.sb.UserRemote;

/**
 *  The timeReport ManagedBean is responsible for connection to the Enterprise Beans
 *  and collecting UserGroups.
 *
 */
public class UserGroupsMB {

    @EJB
    private UserRemote userBean;
    private Collection<UserGroupsEB> userGroups;

    public UserGroupsMB() {
        this.onInit();
    }

    @PostConstruct
    public void onInit() {
        this.userGroups = null;
    }

    //loading user groups from database
    public void loadUserGroups() {
        this.userGroups = userBean.getUserGroups();
    }

    //get an list for a select list in icefaces
    public ArrayList<SelectItem> getUserGroups() {
        problemSolver();
        this.loadUserGroups();
        ArrayList<SelectItem> itemList = new ArrayList<SelectItem>();
        ArrayList<UserGroupsEB> ugs = new ArrayList(userGroups);

        for (UserGroupsEB ug : ugs) {
            SelectItem a = new SelectItem();
            a.setLabel(ug.getGroupName());
            a.setValue(ug.getId().toString());
            itemList.add(a);
        }
        return itemList;
    }

    //spring security bug fix
    private void problemSolver()
    {
        if(this.userBean==null)
        {
            this.userBean = ConnectFactory.getUserBean();
        }
    }
}
